﻿// Author: Iulian Lita
// Project: UsersModule.UserActions
// Path: C:\Users\Tym\Documents\Visual Studio 2008\Projects\Library\UsersModule.UserActions
// Creation date: 1/13/2009 6:28 PM
// Last modified: 1/14/2009 7:26 AM

#region Using directives
using System;
using UsersModule;
#endregion

namespace UsersModule.UserActions
{
    public class DefaultUserActions
    {
        #region Private Fields
        private User _currentUser;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the DefaultUserActions class.
        /// </summary>
        /// <param name="currentUser">Current user</param>
        public DefaultUserActions(User currentUser)
        {
            _currentUser = currentUser;
        } // DefaultUserActi)
        #endregion

        #region Public Methods

        /// <summary>
        /// Recommend item
        /// </summary>
        /// <param name="idItem">Id item</param>
        public bool RecommendItem(int idItem)
        {

            if (_currentUser.UserType == UserTypes.EXTERN)
                return false;
            try
            {
                DatabaseModule.DatabaseWork databaseConnection = new DatabaseModule.DatabaseWork();

                databaseConnection.AddRecomandation(idItem);

                databaseConnection.Dispose();
            }
            catch (Exception exc)
            {
                Error.ErrorManagement.ManageException(exc);
                return false;
            }
            return true;
        } // RecommendItem(idItem)

        /// <summary>
        /// Loan item
        /// </summary>
        /// <param name="idItem">Id item</param>
        public bool LoanItem(int idProduct)
        {
            if (_currentUser.Penalized)
                return false;
            try
            {
                DatabaseModule.DatabaseWork databaseConnection = new DatabaseModule.DatabaseWork();
                int idItem = databaseConnection.GetIdItemFromIdProduct(idProduct);
                databaseConnection.AddLoan(_currentUser.IdUser, idItem);
                databaseConnection.UpdateAvailibility(idItem);
                databaseConnection.Dispose();
            }
            catch (Exception exc)
            {
                Error.ErrorManagement.ManageException(exc);
                return false;
            }

            return true;
        } // LoanItem(idItem)
        #endregion


    }
}
